package com.apanal.qlife.sys.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.apanal.qlife.common.constants.Constants;
import com.apanal.qlife.common.page.Page;
import com.apanal.qlife.common.page.PageReturn;
import com.apanal.qlife.common.util.IdWorkerFactory;
import com.apanal.qlife.sys.dao.CheckcodeMapper;
import com.apanal.qlife.sys.model.Checkcode;
import com.apanal.qlife.sys.service.CheckcodeService;

/**
 * 验证码服务层接口实现类
 * 
 * @author shuliangxing
 * 
 * @date 2015-1-14上午11:36:08
 */
@Service
public class CheckcodeServiceImpl implements CheckcodeService {

	@Autowired
	CheckcodeMapper mapper;

	@Override
	public String deleteById(Integer id) {
		mapper.deleteByPrimaryKey(id);
		return Constants.DEFAULT_MSG_SUCCESS;
	}

	@Override
	public String insert(Checkcode record) {
		record.setCodId(IdWorkerFactory.nextId());
		mapper.insert(record);
		return Constants.DEFAULT_MSG_SUCCESS;
	}

	@Override
	public String insertInLog(Checkcode record) {
		record.setCodId(IdWorkerFactory.nextId());
		mapper.insert(record);
		return Constants.DEFAULT_MSG_SUCCESS;
	}

	@Override
	public String insertSelective(Checkcode record) {
		mapper.insertSelective(record);
		return Constants.DEFAULT_MSG_SUCCESS;
	}

	@Override
	public Checkcode selectById(Integer id) {
		return mapper.selectByPrimaryKey(id);
	}

	@Override
	public String updateByIdSelective(Checkcode record) {
		mapper.updateByPrimaryKeySelective(record);
		return Constants.DEFAULT_MSG_SUCCESS;
	}

	@Override
	public String updateById(Checkcode record) {
		mapper.updateByPrimaryKey(record);
		return Constants.DEFAULT_MSG_SUCCESS;
	}

	@Override
	public List<Checkcode> selectAll() {
		return mapper.select(null);
	}

	@Override
	public PageReturn<Checkcode> selectForPage(Checkcode entity, Page page) {
		int count = mapper.selectForPageCount(entity, page);
		List<Checkcode> list = mapper.selectForPage(entity, page);
		return new PageReturn<Checkcode>(list, count, page, entity);
	}

	@Override
	public Checkcode findNearestCode(String mobile, String code, int type) {
		return mapper.findNearestCode(mobile, code, type);
	}

	@Override
	public void changeCodeUsed(Long id) {
		Checkcode checkcode = new Checkcode();
		checkcode.setCodId(id);
		checkcode.setCodUsed(true);
		checkcode.setCodCheckTime(new Date());
		updateByIdSelective(checkcode);
	}

}
